import "package:ct_pa_majordomo/config/theme_vars.dart";
import "package:ct_pa_majordomo/pages/device/collect/api.dart";
import "package:ct_pa_majordomo/pages/device/gateway/api.dart";
import "package:ct_pa_majordomo/widgets/divider/solid_divider.dart";
import "package:ct_pa_majordomo/widgets/empty_data/index.dart";
import "package:flutter/cupertino.dart";
import "package:flutter/material.dart";
import "package:flutter_easyloading/flutter_easyloading.dart";

class BoundAmmeters extends StatefulWidget {
  final String collectId;
  const BoundAmmeters({super.key, required this.collectId});

  @override
  State<StatefulWidget> createState() =>_BoundAmmetersState();


}

class _BoundAmmetersState extends State<BoundAmmeters> {

  List _list = [];

  Future<void> _getCollectBindAmmeterList() async {
    EasyLoading.show();
    Map resData = await collectApi.collectBindAmmeterList(widget.collectId);
    EasyLoading.dismiss();
    print(resData["data"]);
    try {
      setState(() {
        _list = resData["data"] ?? [];
      });
    } catch(error) {
      print(error);
    }
  }

  @override
  void initState() {
    _getCollectBindAmmeterList();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        iconTheme: const IconThemeData(color: Colors.white),
        title: const Text("绑定的电表", style: TextStyle(color: Colors.white),),
        centerTitle: true,
        backgroundColor: ThemeVars.colorPrimary,
        leading: IconButton(
          icon: const Icon(Icons.arrow_back_ios_new),
          onPressed: () {
            Navigator.pop(context);
          },
        ),
      ),
      body: RefreshIndicator(
        onRefresh: _getCollectBindAmmeterList,
        child: _list.isEmpty ? ListView(
          children: [EmptyData()],
        ) : ListView.separated(
          itemBuilder: (context, index) {
            return Container(
              padding: const EdgeInsets.all(16),
              decoration: const BoxDecoration(
                color: Colors.white,
              ),
              child: Row(
                children: [
                  Container(
                    padding: const EdgeInsets.all(8),
                    decoration: const BoxDecoration(
                        borderRadius: BorderRadius.all(Radius.circular(50)),
                        boxShadow: [BoxShadow(spreadRadius: 2, color: ThemeVars.colorPrimaryLight)],
                        color: Colors.white
                    ),
                    child: const Icon(Icons.lock_open, size: 20,),
                  ),
                  const SizedBox(width: 16,),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text(_list[index]["name"], style: TextStyle(fontSize: 16, fontWeight: FontWeight.w500),),
                      SizedBox(height: 4,),
                      Text(_list[index]["status"] == "2" ? "在线" : "离线", style: TextStyle(color: _list[index]["status"] == "2" ? ThemeVars.colorSuccess : ThemeVars.colorDisabled, fontSize: 12),)
                    ],
                  )
                ],
              ),
            );
          },
          separatorBuilder: (context, index) => const SolidDivider(),
          itemCount: _list.length,
        ),
      ),
    );
  }

}